using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class forums_controls_threads_ThreadPage : Controls.DataManagerThreadControl
{
    private DAL.Post _currentPost;
    public DAL.Post CurrentPost
    {
        get
        {
            return _currentPost;
        }
        set
        {
            _currentPost = value;
        }
    }

    private int _currentPage;
    public int CurrentPage
    {
        get
        {
            return _currentPage;
        }
        set
        {
            _currentPage = value;
        }
    }

    protected override void UserLoggedIn(object sender, EventArgs e)
    {
        Refresh();
    }

    protected override void UserLoggedOut(object sender, EventArgs e)
    {
        Refresh();
    }

    protected override void ItemChanged()
    {
        Refresh();
    }

    private void Refresh()
    {
        bool showDeletedThread = false;
        if (Managers.UserLoginManager.CurrentUser != null &&
            Managers.UserLoginManager.CurrentUser.Status == DAL.User.UserStatus.Banned)
        {
            showDeletedThread = true;
        }

        if (CurrentThread != null &&
            (CurrentThread.Status != DAL.Thread.ThreadStatus.Deleted || showDeletedThread) &&
            Helpers.ForumSecurityHelper.CurrentUserCanViewForum(CurrentThread.Forum))
        {
            mviewThread.SetActiveView(viewPosts);
            SetPostDataSource();

            Moderatethread1.CurrentThread = CurrentThread;

            Breadcrumb1.FooterText = Helpers.StringHelper.SafeFormat(CurrentThread.FirstPost.Subject);
            Breadcrumb1.FooterUrl = Helpers.StringHelper.SafeFormat("~/forums/thread.aspx?t={0}", CurrentThread.ID);

            Breadcrumb1.CurrentForum = CurrentThread.Forum;
            CurrentThread.TotalViews += 1;
            DAL.Threads.Save(CurrentThread);
        }
        else
        {
            Breadcrumb1.FooterText = null;
            Breadcrumb1.CurrentForum = null;
            mviewThread.SetActiveView(viewNoThread);
        }
    }

    private void SetPostDataSource()
    {
        int page = GetCurrentPage();
        ThreadHeaderFooterTop.CurrentPage = page;
        ThreadHeaderFooterTop.CurrentThread = CurrentThread;
        ThreadHeaderFooterBottom.CurrentPage = page;
        ThreadHeaderFooterBottom.CurrentThread = CurrentThread;

        DAL.DataManagerPage<DAL.Post> myPosts = DAL.Posts.GetByThreadAndPage(CurrentThread, page, 25);
        ThreadPosts1.SetDataSource(myPosts.Items);
    }

    private int GetCurrentPage()
    {
        int pageNumber;
        if (Helpers.QueryStringHelper.TryGetInt32("page", out pageNumber))
        {
            return pageNumber;
        }
        else
        {
            if (CurrentPost != null)
            {
                return DAL.Posts.GetPageNumberFromPost(CurrentPost, 25);
            }
            else
            {
                return 1;
            }
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            TryGetPostFromQueryString();
            if (CurrentPost != null)
            {
                CurrentThread = CurrentPost.Thread;
            }
            else
            {
                CurrentThread = GetItemFromQueryString();
            }
        }
        if (CurrentThread != null)
        {
            Page.Title = "FSTDT Forums: " + Helpers.StringHelper.SafeFormat(CurrentThread.FirstPost.Subject);
        }
        else
        {
            Page.Title = "FSTDT Forums: " + "Thread not found";
        }
    }
    private void TryGetPostFromQueryString()
    {
        long postID;
        if (Helpers.QueryStringHelper.TryGetInt64("p", out postID))
        {
            CurrentPost = DAL.Posts.Item(postID);
        }
        else
        {
            CurrentPost = null;
        }
    }

    protected void ThreadUpdated(object sender, EventArgs e)
    {
        Refresh();
    }
}
